package juc_11_pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecutorsDemo01 {

    public static void main(String[] args) {

        // ExecutorService executorService = Executors.newSingleThreadExecutor();
        // ExecutorService executorService = Executors.newFixedThreadPool(3);
        ExecutorService executorService = Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 6; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName());
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

        System.out.println("main 1 ...");

        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        executorService2.execute(()->{
            System.out.println(Thread.currentThread().getName());
        });
        executorService2.shutdown();

        System.out.println("main 2 ...");

    }
}
